﻿//===============================================================================
//	LuaScriptManager.h
//	lua脚本管理器
//===============================================================================

#pragma once

#include <memory>
#include <cstdint>
#include <sstream>
#include <mutex>

#include "LuaManager.h"
#include "../share/user_define.h"

//-------------------------------------------------------------------------------
struct ScriptInfo
{
	static int32_t const invalid_int32_value = -1;
	static uint32_t const size_128 = 128;

	ScriptInfo(void)
		: m_idx(invalid_int32_value)
		, m_type(invalid_int32_value)
		, m_script_id(invalid_int32_value)
	{
		memset(m_filename, 0, size_128);
	}

	void Clear(void)
	{
		m_idx = invalid_int32_value;
		m_type = invalid_int32_value;
		m_script_id = invalid_int32_value;
		memset(m_filename, 0, size_128);
	}

	void Initialize(std::stringstream& ss)
	{
		Clear();

		ss >> m_idx >> m_script_id >> m_type >> m_filename;
	}

	int32_t	m_idx;
	int32_t	m_type;
	uint32_t m_script_id;
	uint32_t m_mission_id;
	char	m_filename[size_128];
};

//-------------------------------------------------------------------------------
class LuaScriptManager
{
	static uint32_t const size_1024 = 1024;
	static uint32_t const size_16 = 16;

public:
	LuaScriptManager(void);
	~LuaScriptManager(void);

public:
	bool Initialize(int32_t hostId);
	char* GetFileNameByScriptId( uint32_t scriptID );
	bool UpdateToLuaManager(lua_manager_ptr& lmp);
	bool reload(uint32_t scriptID);
	void Clear(void);

private:
	std::recursive_mutex m_lua_mutex;
	lua_manager_ptr m_ptr[size_16];
	ScriptInfo	m_script_info[size_1024];
	int32_t m_script_count;
	int32_t m_ptr_count;

	int32_t m_game_host_id;
};

//-------------------------------------------------------------------------------
extern std::shared_ptr<LuaScriptManager> g_lua_script_manager;